package com.dataart.productshop.controller;

import com.dataart.productshop.Constants;
import com.dataart.productshop.model.boiko.informationsystem.Kathedra;
import com.dataart.productshop.model.boiko.informationsystem.Student;
import com.dataart.productshop.service.KathedraManager;
import com.dataart.productshop.service.StudentManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class StudentController {

    @Autowired
    StudentManager studentManager;

    @Autowired
    KathedraManager kathedraManager;

    @RequestMapping(value = "/students", method = RequestMethod.GET)
    public ModelAndView studentPageInfo(){
        return new ModelAndView("students", "items", studentManager.getAll());
    }

    @RequestMapping(value = "/students/addnew", method = RequestMethod.GET)
    public ModelAndView requestAddingNewStudent(){
        return new ModelAndView("addStudent", "kathedraList", kathedraManager.getAll());
    }

    @RequestMapping(value = {"/students/addnew",
            "/edit" + Constants.STUDENT_TYPE + "/{itemId}"},
            method = RequestMethod.POST)
    public String createNewStudent(
            @RequestParam(value = "name", required = false)String name,
            @RequestParam(value = "id", required = false)String id,
            @RequestParam(value = "kathedra", required = false)String kathedraId,
            @RequestParam(value = "enterDate", required = false) String enterDateString,
            @RequestParam(value = "finishDate", required = false)String finishDateString){
        if(kathedraId != null && enterDateString != null && finishDateString != null
                && name != null && !name.isEmpty() && noStudentWithSuchName(name)){
            try{
                Kathedra k = new Kathedra();
                k.setId(Long.parseLong(kathedraId));

                Date enterDate = Constants.DATE_FORMAT.parse(enterDateString.trim());
                Date finishDate = Constants.DATE_FORMAT.parse(finishDateString.trim());

                if(!enterDate.before(finishDate)){
                    System.err.println("sddd");
                    System.err.println(enterDate);
                    System.err.println(finishDate);
                    return "redirect:/students/addnew";
                }

                Student st = new Student();
                if(id != null && !id.isEmpty()){
                    st = studentManager.getById(Long.parseLong(id));
                }
                st.setName(name);
                st.setDateEnter(enterDate);
                st.setDateFinish(finishDate);
                st.setKathedra(k);

                studentManager.save(st);
            }catch(NumberFormatException ex){
                System.err.println("NumberFormatException");
                return "redirect:/students/addnew";
            } catch (ParseException e) {
                System.err.println("ParseException");
                return "redirect:/students/addnew";
               // e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

        }
        return "redirect:/students";
    }


    @RequestMapping(value = "/remove" + Constants.STUDENT_TYPE + "/{itemId}"
            , method = RequestMethod.GET)
    public String removeStudent(@PathVariable("itemId") String itemId){

        try{
            studentManager.delete(Long.parseLong(itemId));
        }catch (NumberFormatException e){

        }catch(Exception dd){
            System.err.println("some exception were occured in AssKathedra controller"
                    + dd.getStackTrace());
        }
        return "redirect:/students";

    }


    @RequestMapping(value = "/edit" + Constants.STUDENT_TYPE + "/{itemId}"
            , method = RequestMethod.GET)
    public ModelAndView editStudent(@PathVariable("itemId") String itemId){

        try{
            Student st = studentManager.getById(Long.parseLong(itemId));
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("kathedraList", kathedraManager.getAll());
            map.put("student", studentManager.getById(Long.parseLong(itemId)));
            return new ModelAndView("addStudent", map);

        }catch (NumberFormatException e){

        }catch(Exception dd){
            System.err.println("some exception were occured in AssKathedra controller"
                    + dd.getStackTrace());
        }
        return new ModelAndView("students", "items", studentManager.getAll());
    }




    private boolean noStudentWithSuchName(String name){
        return studentManager.getByName(name).isEmpty();
    }




}
